package com.ordering.action.user.login;

import java.security.NoSuchAlgorithmException;

import com.ordering.action.BaseAction;
import com.ordering.entity.User;
import com.ordering.entity.UserInfo;
import com.ordering.service.login.UserLoginService;
import com.ordering.service.login.impl.UserLoginServiceImpl;
import com.ordering.util.MD5;

public class UserLoginAction extends BaseAction
{
    //输入
    private String msisdn;
    private String password;
    private String imageCode;
    //输出
    private String message;
    private User user;
    UserLoginService service = new UserLoginServiceImpl();
    
    public String login() {
        try
        {
            User po = new User();
            po.setMsisdn(msisdn.trim());
            po.setPassword(MD5.crypt(password.trim()));
            user = service.login(po);
            if(user == null){
                message = "用户名或密码错误，请重新登录";
                return FAIL;
            }else {
            	//封装UserInfo对象并放入session中
            	UserInfo userInfo = new UserInfo();
            	userInfo.setId(user.getId());
            	userInfo.setPassword(user.getPassword());
            	userInfo.setUsername(user.getName());
            	userInfo.setUserRole(UserInfo.USER_ROLE_USER);
                session.put("user", userInfo);
                return "login";
            }
        }
        catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        }
        return SUCCESS;
    }

    
    
    public String logout() {
        session.remove("user");
        
        return "logout";
    }
    
    
    
    
    
    
    
    
    
    
    
    
    public String getMsisdn()
    {
        return msisdn;
    }

    public void setMsisdn(String msisdn)
    {
        this.msisdn = msisdn;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public String getImageCode()
    {
        return imageCode;
    }

    public void setImageCode(String imageCode)
    {
        this.imageCode = imageCode;
    }

    public String getMessage()
    {
        return message;
    }

    public void setMessage(String message)
    {
        this.message = message;
    }

    public User getUser()
    {
        return user;
    }

    public void setUser(User user)
    {
        this.user = user;
    }
}
